package com.bkfm.framework.wf.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yak.core.service.BaseService;
import org.yak.core.web.ResultSet;

import com.bkfm.framework.core.api.service.IBkfmTableMetaServer;
import com.bkfm.framework.core.entity.BkfmFilter;
import com.bkfm.framework.wf.api.service.IWorkFlowTaskService;
import com.bkfm.framework.wf.api.store.IWorkFlowStore;
import com.bkfm.framework.wf.constants.WorkFlowConstants;
import com.bkfm.framework.wf.entity.BkfmDeployedWorkFlow;
import com.bkfm.framework.wf.entity.BkfmWorkFlow;
import com.bkfm.framework.wf.entity.BkfmWorkFlowForm;
import com.bkfm.framework.wf.entity.BkfmWorkFlowLine;
import com.bkfm.framework.wf.entity.BkfmWorkFlowNode;
import com.bkfm.framework.wf.entity.task.BkfmWorkFlowTaskNodeInfo;

/**
 * 
 * @平台：BKFM统一开发平台
 * @文件名称：WorkFlowTaskService.java
 * @文件描述：工作流业务服务类
 * @author XQ
 * @日期：2019年1月31日下午1:22:07
 *
 */
@Service
@Transactional
public class WorkFlowTaskService extends BaseService<BkfmDeployedWorkFlow> implements IWorkFlowTaskService{

	@Resource
	private IWorkFlowStore workFlowStore;
	
	@Resource
	private IBkfmTableMetaServer tableMetaService;
	
	/**
	 * 获取开始任务节点
	 * @return BkfmWorkFlowTaskNodeInfo 返回任务节点对应的相关信息
	 * @throws Exception
	 */
	public BkfmWorkFlowTaskNodeInfo getStartTask(BkfmDeployedWorkFlow deployedWorkFlow)throws Exception{
		BkfmFilter bkfmFilter = new BkfmFilter();
		//查询流程开始节点
		bkfmFilter.setObj_path("ROOT/WORKFLOW/LCGL/BLK_WORKFLOW_NODES");
		bkfmFilter.setFilter(" DEPLOYED_WORKFLOW_ID='"+deployedWorkFlow.getRwid()+"' AND NODE_TYPE='"+WorkFlowConstants.CELL_TYPES_START+"'");
		ResultSet resultSet = tableMetaService.queryTableModelMetaResultSet(bkfmFilter);
		BkfmWorkFlowNode startWorkFlowNode= (BkfmWorkFlowNode)resultSet.trans2Bean(BkfmWorkFlow.class);
		//查询流程开始动作连接线
		bkfmFilter.setObj_path("ROOT/WORKFLOW/LCGL/BLK_WORKFLOW_NODE_ACTIONS");
		bkfmFilter.setFilter(" DEPLOYED_WORKFLOW_ID='"+deployedWorkFlow.getRwid()+"' AND SOURCENODE_CODE='"+startWorkFlowNode.getNode_code()+"'");
		List<BkfmWorkFlowLine> bkfmWorkFlowLineList = new ArrayList<BkfmWorkFlowLine>();
		tableMetaService.queryTableModelMetaResultSetList(bkfmFilter).forEach((resultSetItem)->{
			BkfmWorkFlowLine bkfmWorkFlowLine= (BkfmWorkFlowLine)resultSetItem.trans2Bean(BkfmWorkFlow.class);
			bkfmWorkFlowLineList.add(bkfmWorkFlowLine);
		});
		
		//查询表单
		bkfmFilter = new BkfmFilter();
		bkfmFilter.setObj_path("ROOT/WORKFLOW/LCGL/BLK_WORKFLOW_FORMS");
		bkfmFilter.setFilter(" DEPLOYED_WORKFLOW_ID='"+deployedWorkFlow.getRwid()+"' AND WORKFLOW_FORM_NAME='"+startWorkFlowNode.getNode_form_name()+"'");
		resultSet = tableMetaService.queryTableModelMetaResultSet(bkfmFilter);
		BkfmWorkFlowForm bkfmWorkFlowForm= (BkfmWorkFlowForm)resultSet.trans2Bean(BkfmWorkFlow.class);
		//查询业务表
		
		
		BkfmWorkFlowTaskNodeInfo bkfmWorkFlowTaskNodeInfo = new BkfmWorkFlowTaskNodeInfo();
		bkfmWorkFlowTaskNodeInfo.setBkfmWorkFlowNode(startWorkFlowNode);
		bkfmWorkFlowTaskNodeInfo.setBkfmWorkFlowLineList(bkfmWorkFlowLineList);
		bkfmWorkFlowTaskNodeInfo.setBkfmWorkFlowForm(bkfmWorkFlowForm);
		return bkfmWorkFlowTaskNodeInfo;
	}
}
